#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc_core.h"

/*RM9350 Gamma??′??÷???·*/
u8 RM350_Gamma_r[56]=
{
    0x00,0x01,  //r0
    0x02,0x03,  //r1
    0x04,0x05,  //r2
    0x06,0x07,  //r3
    0x08,0x09,  //r7
    0x0A,0x0B,  //r11
    0x0c,0x0D,  //r15
    0x0e,0x0f,  //r19
    0x10,0x11,  //r23
    0x12,0x13,  //r27
    0x14,0x15,  //r31
    0x16,0x17,  //r35
    0x18,0x19,  //r39
    0x1a,0x1b,  //r47
    0x1C,0x1D,  //r55
    0x1e,0x1f,  //r63
    0x20,0x21,  //r79
    0x22,0x23,  //r95
    0x24,0x25,  //r111
    0x26,0x27,  //r127
    0x28,0x29,  //r143
    0x2a,0x2b,  //r159
    0x30,0x31,  //r175
    0x32,0x33,  //r191
    0x34,0x35,  //r207
    0x36,0x37,  //r223
    0x38,0x39,  //r239
    0x3A,0x3B   //r255
};
u8 RM350_Gamma_g[56]=
{
    0x40,0x41,  //g0
    0x42,0x43,  //g1
    0x44,0x45,  //g2
    0x46,0x47,  //g3
    0x48,0x49,  //g7
    0x4A,0x4B,  //g11
    0x4c,0x4d,  //g15
    0x4E,0x4F,  //g19
    0x50,0x51,  //g23
    0x52,0x53,  //g27
    0x58,0x59,  //g31
    0x5a,0x5b,  //g35
    0x5c,0x5d,  //g39
    0x5e,0x5f,  //g47
    0x60,0x61,  //g55
    0x62,0x63,  //g63
    0x64,0x65,  //g79
    0x66,0x67,  //g95
    0x68,0x69,  //g111
    0x6A,0x6B,  //g127
    0x6C,0x6D,  //g143
    0x6E,0x6F,  //g159
    0x70,0x71,  //g175
    0x72,0x73,  //g191
    0x74,0x75,  //g207
    0x76,0x77,  //g223
    0x78,0x79,  //g239
    0x7A,0x7b   //g255
};
u8 RM350_Gamma_b[56]={
    0x7c,0x7D,  //b0
    0x7E,0x7f,  //b1
    0x80,0x81,  //b2
    0x82,0x83,  //b3
    0x84,0x85,  //b7
    0x86,0x87,  //b11
    0x88,0x89,  //b15
    0x8A,0x8b,  //b19
    0x8c,0x8d,  //b23
    0x8e,0x8f,  //b27
    0x90,0x91,  //b31
    0x92,0x93,  //b35
    0x94,0x95,  //b39
    0x96,0x97,  //b47
    0x98,0x99,  //b55
    0x9A,0x9b,  //b63
    0x9c,0x9d,  //b79
    0x9e,0x9f,  //b95
    0xa4,0xa5,  //b111
    0xa6,0xa7,  //b127
    0xac,0xad,  //b143
    0xae,0xaf,  //b159
    0xb0,0xb1,  //b175
    0xb2,0xb3,  //b191
    0xb4,0xb5,  //b207
    0xb6,0xb7,  //b223
    0xb8,0xb9,  //b239
    0xbA,0xbb   //b255
};

/********************************************************************************************
********************************************************************************************
    RM69350  D′??′??÷ ?á??′??÷  D′Gamma ?áGamma OTPé???
    RM69350  D′??′??÷ ?á??′??÷  D′Gamma ?áGamma OTPé???
    RM69350  D′??′??÷ ?á??′??÷  D′Gamma ?áGamma OTPé???
    RM69350  D′??′??÷ ?á??′??÷  D′Gamma ?áGamma OTPé???
    RM69350  D′??′??÷ ?á??′??÷  D′Gamma ?áGamma OTPé???
********************************************************************************************
********************************************************************************************/
void RM6935x_Write_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFfd);
		//SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
    delay_ms(15); 
    SSD2828_W_Array(SigMode,channel,buffer,2);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    delay_ms(5);
    buffer[4] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? D′??′??÷×′ì?  ￡ook
}

void RM6935x_Read_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[2];
		SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);  //bit7 1:?á2ù×÷ bit4 1￡oHS CLOCK Disable
    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);                                
    buffer1[0] = 0x01;
    buffer1[1] = buffer[3];
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
    delay_ms(20);
    buffer[4]=SSD2828_R_Reg(SigMode,channel,0xFF);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    delay_ms(5);
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? ?áè?μ? ??′??÷êy?Y  ￡ook  
}

void RM6935x_Write_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[5];
    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFfd);
		delay_ms(5);

    if((buffer[3]&0x03) == 0x00)
    {
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);     //UCS 
        buffer1[0] = 0x02;
        buffer1[1] = 0x38;
        buffer1[2] = 0x00;                             //exit Idle Mode
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
        buffer1[0] = 0x02;
        buffer1[1] = 0x53;
        buffer1[2] = 0x20;                              //select HBM and Enable BC Control
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x50;                // GAMMA1  page1
        SSD2828_W_Array(SigMode,channel,buffer1,0);
    }
	
    else if((buffer[3]&0x03) == 0x01)
    {
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        buffer1[0] = 0x02;
        buffer1[1] = 0x53;
        buffer1[2] = 0x20;                           //Select HBM and Enable BC Control
        SSD2828_W_Array(SigMode,channel,buffer1,0);  
        buffer1[0] = 0x02;
        buffer1[1] = 0x39;
        buffer1[2] = 0x00;                          //enter Idle Mode
        SSD2828_W_Array(SigMode,channel,buffer1,0);   
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x52;                // GAMMA2  page7
        SSD2828_W_Array(SigMode,channel,buffer1,0);
    }   
    else if((buffer[3]&0x03) == 0x02)
    {
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        buffer1[0] = 0x02;
        buffer1[1] = 0x38;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
        buffer1[0] = 0x02;
        buffer1[1] = 0x53;
        buffer1[2] = 0x60;
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
			  buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x40;
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
			  buffer1[0] = 0x02;
        buffer1[1] = 0x6B;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x54;                // GAMMA3  page8
        SSD2828_W_Array(SigMode,channel,buffer1,0);
    }
		else
		{
				buffer[4] = Uart_Error_Oth;
				STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? D′Gamma×′ì?  ￡ook  				
				goto RM69350_w_end;
		}

    for( i = 0;i<buffer[2]-1;i++) 
    {	 
        if((buffer[3]&0xf0)==0x00)	
            SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM350_Gamma_r[i]);  
        else if((buffer[3]&0xf0)==0x40)
            SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM350_Gamma_g[i]); 
        else if((buffer[3]&0xf0)==0x80)
            SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM350_Gamma_b[i]); 																		

        delay_ms(3);
    }

    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? D′Gamma×′ì?  ￡ook  
		RM69350_w_end: ;
}

void RM6935x_Read_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[3];
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15);
		buffer1[0] = 0x02;
        
    if(buffer[4] == 0x00)      //Gamma1
    {
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        buffer1[0] = 0x02;
        buffer1[1] = 0x38;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0); 

        buffer1[0] = 0x02;
        buffer1[1] = 0x53;
        buffer1[2] = 0x20;
        SSD2828_W_Array(SigMode,channel,buffer1,0);          

        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
			  buffer1[2] = 0x50;      // GAMMA1  page1
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
    }
    else if(buffer[4] == 0x01) //Gamma2
    {
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        buffer1[0] = 0x02;
        buffer1[1] = 0x53;
        buffer1[2] = 0x20;
        SSD2828_W_Array(SigMode,channel,buffer1,0);  

        buffer1[0] = 0x02;
        buffer1[1] = 0x39;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);  

        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;       
        buffer1[2] = 0x52;      // GAMMA2  page7
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
    }
    else if(buffer[4] == 0x02) //Gamma3
    {
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);	
        buffer1[0] = 0x02;
        buffer1[1] = 0x38;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);  
        buffer1[0] = 0x02;
        buffer1[1] = 0x53;
        buffer1[2] = 0x60;
        SSD2828_W_Array(SigMode,channel,buffer1,0);         
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;        
        buffer1[2] = 0x54;      // GAMMA3  page8
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
    }
		else
		{
				buffer[4] = Uart_Error_Oth;
				STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? D′Gamma×′ì?  ￡ook  				
    		goto RM69350_r_end;
		}		
		delay_ms(15);		
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
    SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);	
    for( i = 0;i<buffer[2]-1;i++)			 	
    {  	
        
        SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);                                
        buffer1[0] = 0x01;
        buffer1[1] = (buffer[3]==0x00) ? RM350_Gamma_r[i] : ((buffer[3]==0x42) ? RM350_Gamma_g[i] : RM350_Gamma_b[i]);
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
				delay_ms(6);
        buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);        
    }	
    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? ?áè?μ? Gammaêy?Y  ￡ook  
		RM69350_r_end: ;
}

void RM6935x_Gamma_OTP_Start(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u16 tmp;
    u8 buffer1[3];  
		u8 Otptimes=buffer[4];
    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);   
    delay_ms(5);
    buffer1[0] = 0x02;
    buffer1[1] = 0xFE;
    buffer1[2] = 0x00;
    SSD2828_W_Array(SigMode,channel,buffer1,0);     // UCSó??§????
    delay_ms(5);
    buffer1[0] = 0x01;
    buffer1[1] = 0x28;
    SSD2828_W_Array(SigMode,channel,buffer1,0);     // 1???ê?
    delay_ms(100);
    buffer1[0] = 0x01;
    buffer1[1] = 0x10;
    SSD2828_W_Array(SigMode,channel,buffer1,0);     // 1???ê?     
    delay_ms(100);  //otp
    buffer1[0] = 0x02;
    buffer1[1] = 0xFE;
    buffer1[2] = 0x40;
	  //buffer1[2] = 0x04;
    SSD2828_W_Array(SigMode,channel,buffer1,0);     //Page0
    buffer1[0] = 0x02;//external OTP power
    buffer1[1] = 0xF2;
    buffer1[2] = 0x02;
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(150);
    if(buffer[3]==0x00)  //otp GammaSet1
    {
        if(Otptimes == 1)      // 1st
        {
            buffer1[1] = 0xF6;
            buffer1[2] = 0x02;
        }
        else if(Otptimes == 2) // 2nd
        {
            buffer1[1] = 0xF7;
            buffer1[2] = 0x02;        
        }
        SSD2828_W_Array(SigMode,channel,buffer1,0);     //OTP_CMD2_P1_PGM_EN          
    }
    if(buffer[3]==0x01)  //otp GammaSet2
    {
        if(Otptimes == 1)      // 1st
        {
            buffer1[1] = 0xF6;
            buffer1[2] = 0x40;
        }
        else if(Otptimes == 2) // 2nd
        {
            buffer1[1] = 0xF7;
            buffer1[2] = 0x40;        
        }
        SSD2828_W_Array(SigMode,channel,buffer1,0);     //OTP_CMD2_P6_PGM_EN  
    }
    if(buffer[3]==0x02) //otp GammaSet3
    {		
        if(Otptimes == 1)      // 1st
        {
            buffer1[1] = 0xF6;
            buffer1[2] = 0x80;
        }
        else if(Otptimes == 2) // 2nd
        {
            buffer1[1] = 0xF7;
            buffer1[2] = 0x80;        
        }        
        SSD2828_W_Array(SigMode,channel,buffer1,0);     //OTP_CMD2_P8_PGM_EN  
    }
    if(buffer[3]==0x03) // otp goa
    {
        buffer1[0] = 0x02;
        if(Otptimes == 1)      // 1st
        {
            buffer1[1] = 0xF6;
            buffer1[2] = 0x04;;
        }
        else if(Otptimes == 2) // 2nd
        {
            buffer1[1] = 0xF7;
            buffer1[2] = 0x04;        
        }
        else if(Otptimes == 3) // 3rd
        {
            buffer1[1] = 0xF9;
            buffer1[2] = 0x80;        
        }
        SSD2828_W_Array(SigMode,channel,buffer1,0);     //OTP_CMD2_P2_PGM_EN  
    }
    if(buffer[3]==0x10) // otp power
    {
        buffer1[0] = 0x02;        
        if(Otptimes == 1)      // 1st
        {
            buffer1[1] = 0xF6;
            buffer1[2] = 0x01;
        }
        else if(Otptimes == 2) // 2nd
        {
            buffer1[1] = 0xF7;
            buffer1[2] = 0x01;        
        }
        else if(Otptimes == 3) // 3rd
        {
            buffer1[1] = 0xF9;
            buffer1[2] = 0x40;        
        }        
        SSD2828_W_Array(SigMode,channel,buffer1,0);     //OTP_CMD2_P0_PGM_EN  
    }  
    if(buffer[3]==0x06)
		{		buffer[1]=0x06;
			/*
				buffer1[0] = 0x02; 
				buffer1[1] = 0xFE;
				buffer1[2] = 0x20;
				SSD2828_W_Array(SigMode,channel,buffer1,0);
				buffer1[1] = 0x09;
				buffer1[2] = 0x13;
				SSD2828_W_Array(SigMode,channel,buffer1,0);*/
				buffer1[1] = 0xFE;
				buffer1[2] = 0xD2;
				SSD2828_W_Array(SigMode,channel,buffer1,0);			
				buffer1[1] = 0x0E;
				buffer1[2] = 0xC0;
				SSD2828_W_Array(SigMode,channel,buffer1,0);			
				buffer1[1] = 0x0F;
				buffer1[2] = 0x7F;
				SSD2828_W_Array(SigMode,channel,buffer1,0);
				buffer1[1] = 0xAA;
				buffer1[2] = 0x01;
				SSD2828_W_Array(SigMode,channel,buffer1,0);		
	/*
				buffer1[1] = 0x0B;
				buffer1[2] = 0x00;
				SSD2828_W_Array(SigMode,channel,buffer1,0);
				buffer1[1] = 0x0C;
				buffer1[2] = 0x00;
				SSD2828_W_Array(SigMode,channel,buffer1,0);			
				buffer1[1] = 0x0D;
				buffer1[2] = 0xCC;
				SSD2828_W_Array(SigMode,channel,buffer1,0);			
				buffer1[1] = 0x01;
				buffer1[2] = 0xCF;
				SSD2828_W_Array(SigMode,channel,buffer1,0);
				buffer1[1] = 0x19;
				buffer1[2] = 0xC0;
				SSD2828_W_Array(SigMode,channel,buffer1,0);	
				
				buffer1[1] = 0xA0;
				buffer1[2] = 0x2B;
				SSD2828_W_Array(SigMode,channel,buffer1,0);			
				buffer1[1] = 0x06;
				buffer1[2] = 0x23;
				SSD2828_W_Array(SigMode,channel,buffer1,0);			
				buffer1[1] = 0xA4;
				buffer1[2] = 0x40;
				SSD2828_W_Array(SigMode,channel,buffer1,0);
				buffer1[1] = 0x11;
				buffer1[2] = 0x03;
				SSD2828_W_Array(SigMode,channel,buffer1,0);	*/
        if(Otptimes == 1)      // 1st
        {
						buffer1[1] = 0xFE;
						buffer1[2] = 0x40;
						SSD2828_W_Array(SigMode,channel,buffer1,0);
						buffer1[1] = 0xF6;
						buffer1[2] = 0x25;
						SSD2828_W_Array(SigMode,channel,buffer1,0);	
						buffer1[1] = 0xF8;//72 //20
						buffer1[2] = 0x0C;
						SSD2828_W_Array(SigMode,channel,buffer1,0);	
						buffer1[1] = 0xFE;
						buffer1[2] = 0xD0;
						SSD2828_W_Array(SigMode,channel,buffer1,0);
						buffer1[1] = 0xE3; //A0 //D0 //D2
						buffer1[2] = 0x29;
						SSD2828_W_Array(SigMode,channel,buffer1,0);	
				}
				else if (Otptimes == 2)	
				{
						buffer1[1] = 0xFE;
						buffer1[2] = 0x40;
						SSD2828_W_Array(SigMode,channel,buffer1,0);
						buffer1[1] = 0xF7;
						buffer1[2] = 0x25;
						SSD2828_W_Array(SigMode,channel,buffer1,0);	
						buffer1[1] = 0xF9;//72 //20
						buffer1[2] = 0x0C;
						SSD2828_W_Array(SigMode,channel,buffer1,0);	
						buffer1[1] = 0xFE;
						buffer1[2] = 0xD0;
						SSD2828_W_Array(SigMode,channel,buffer1,0);
						buffer1[1] = 0xE4; //A0 //D0 //D2
						buffer1[2] = 0x29;
						SSD2828_W_Array(SigMode,channel,buffer1,0);		
				}					
				else if (Otptimes == 3)
				{
						buffer1[1] = 0xFE;
						buffer1[2] = 0x40;
						SSD2828_W_Array(SigMode,channel,buffer1,0);
						buffer1[1] = 0xF9;
						buffer1[2] = 0xC0;   //PAGE 40/60
						SSD2828_W_Array(SigMode,channel,buffer1,0);	
				}
				delay_ms(1500); 
		}
	
    buffer1[0] = 0x02;//MTPCTR
    buffer1[1] = 0xF2;
    buffer1[2] = 0x03;
    SSD2828_W_Array(SigMode,channel,buffer1,0);    
	
    buffer1[0] = 0x02;
    buffer1[1] = 0xF3;
    buffer1[2] = 0xA5;
    SSD2828_W_Array(SigMode,channel,buffer1,0);  

    buffer1[0] = 0x02;
    buffer1[1] = 0xF4;
    buffer1[2] = 0x5A;
    SSD2828_W_Array(SigMode,channel,buffer1,0);  

    buffer1[0] = 0x02;
    buffer1[1] = 0xF5;
    buffer1[2] = 0x3C;
    SSD2828_W_Array(SigMode,channel,buffer1,0);  		
                      
    delay_ms(1500);         // wait for >400 ms   

    buffer[4] = Uart_Error_Oth;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? D′OTP×′ì?  ￡ook
}

void RM6935x_Read_OTP_Time(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[3];
    u16 ED_D,EE_D,EB_D;
    u8 RM350_otptime = 0;
    if(SigMode != CMD_Mode)
        SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    delay_ms(5);
    buffer1[0] = 0x02;
    buffer1[1] = 0xFE;
    buffer1[2] = 0x40;
    SSD2828_W_Array(SigMode,channel,buffer1,0);     // Page 0  ó?693002?ò???
    buffer1[0] = 0x02;
    buffer1[1] = 0xF2;
    buffer1[2] = 0x80;
    SSD2828_W_Array(SigMode,channel,buffer1,0);     // otp_load_en   ó?69300ò??? 														
    buffer1[0] = 0x02;
    buffer1[1] = 0xF6;
    buffer1[2] = 0x03;
    SSD2828_W_Array(SigMode,channel,buffer1,0);     // Margin Read Check   ó?69300ò??? 	  
    
    SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);      

    delay_ms(5);
    buffer1[0] = 0x01;
    buffer1[1] = 0xEE;         //ó?69300óD??±e
    SSD2828_W_Array(SigMode,channel,buffer1,0);
    delay_ms(20);
    if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI  
        SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);							
    delay_ms(5);                                  
    EE_D=SSD2828_R_Reg(SigMode,channel,0xFF); 
    
    
    buffer1[0] = 0x01;
    buffer1[1] = 0xED;
    SSD2828_W_Array(SigMode,channel,buffer1,0);
    delay_ms(20);
    if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI  
        SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);							
    delay_ms(5);                             
    ED_D=SSD2828_R_Reg(SigMode,channel,0xFF); 
    
    buffer1[0] = 0x01;
    buffer1[1] = 0xEB;
    SSD2828_W_Array(SigMode,channel,buffer1,0);
    delay_ms(20);
    if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI  
        SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);							
    delay_ms(5);                               
    EB_D=SSD2828_R_Reg(SigMode,channel,0xFF);     
    if(buffer[3] == 0x00)       //?áè?gamma1′?êy
    {
        if(EE_D&0X02)
            RM350_otptime = 1;
        if(ED_D&0X02)
            RM350_otptime = 2;
    }
    else if(buffer[3] == 0x01)  //?áè?gamma2′?êy
    {
        if(EE_D&0X02)
            RM350_otptime = 1; 
        if(ED_D&0X40)
            RM350_otptime = 2;        
    }
    else if(buffer[3] == 0x02)  //?áè?gamma3′?êy
    {
        if(EE_D&0X80)
            RM350_otptime = 1;
        if(ED_D&0X80)
            RM350_otptime = 2;        
    }
    else if(buffer[3] == 0x03)  //?áè?GOA′?êy
    {
        if(EE_D&0X04)
            RM350_otptime = 1;
        if(ED_D&0X20)
            RM350_otptime = 2;  
        if(EB_D&0X80)
            RM350_otptime = 3;
        
    }
    else if(buffer[3] == 0x04)  //?áè?POWER′?êy
    {
        if(EE_D&0X01)
            RM350_otptime = 1;
        if(ED_D&0X01)
            RM350_otptime = 2;  
        if(EB_D&0X40)
            RM350_otptime = 3;   
    }                                
    buffer[4]=RM350_otptime;
    RM350_otptime=0x00;
    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[3] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? ?áè?μ?OTPêy?Y  ￡ook  
}

void RM6935x_Gamma_switch(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[5];
    static u8 RM69350_state = 0;    // 0=normal   1=idle  2=HBM
    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    delay_ms(10);
    if(buffer[3] == 0x01)
    {
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);   //UCSó??§??ó?69300ò???
        if(RM69350_state == 2)
        {
            buffer1[0] = 0x02;
            buffer1[1] = 0x53;
            buffer1[2] = 0x20;
            SSD2828_W_Array(SigMode,channel,buffer1,0);
        }
        else if(RM69350_state == 1)
        {
            buffer1[0] = 0x02;
            buffer1[1] = 0x38;
            buffer1[2] = 0x00;
            SSD2828_W_Array(SigMode,channel,buffer1,0);  
        }  
        
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
				buffer1[2] = 0x50;   //MCS Page1 Gamma set-1
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        RM69350_state = 0;
    }
    else if(buffer[3] == 0x02)                       //UCSó??§??ó?69300ò???
    {
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        if(RM69350_state == 2)
        {
            buffer1[0] = 0x02;
            buffer1[1] = 0x53;
            buffer1[2] = 0x20;
            SSD2828_W_Array(SigMode,channel,buffer1,0);
        }
        buffer1[0] = 0x02;
        buffer1[1] = 0x39;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);    
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
				buffer1[2] = 0x52;                             //MCS Page7 Gamma set-2
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        RM69350_state = 1;
       
    }   
    else if(buffer[3] == 0x03)
    {
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        if(RM69350_state == 1)
        {
            buffer1[0] = 0x02;
            buffer1[1] = 0x38;
            buffer1[2] = 0x00;
            SSD2828_W_Array(SigMode,channel,buffer1,0);
        }
        buffer1[0] = 0x02;
        buffer1[1] = 0x53;
        buffer1[2] = 0x60;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
				buffer1[2] = 0x54;     //GAMMA3
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        RM69350_state = 2;
    }
    
//    delay_ms(50);
//    buffer1[0] = 0x02;
//    buffer1[1] = 0xEF;       //ó?693002?ò???
//    buffer1[2] = 0x01;
//    SSD2828_W_Array(SigMode,channel,buffer1,0);
//    delay_ms(50);    
    if(buffer[4] == 0x01) 
    {
        buffer1[0] = 0x02;
        buffer1[1] = 0xBC;
        buffer1[2] = 0x00;                  // 28°óμ?ó?69300ò???
        SSD2828_W_Array(SigMode,channel,buffer1,0);
    }
    else if(buffer[4] == 0x02)
    {
    
        buffer1[0] = 0x02;
        buffer1[1] = 0xBC;
        buffer1[2] = 0x01;                  // 15°óμ?ó?69300ò???
        SSD2828_W_Array(SigMode,channel,buffer1,0);
    }
//		delay_ms(50);
//		buffer1[0] = 0x02;
//		buffer1[1] = 0xF0;
//		buffer1[2] = 0x01;    
//		SSD2828_W_Array(SigMode,channel,buffer1,0);   //?üD?Gamma′óRAMμ???′??÷
//		buffer1[0] = 0x02;
//		buffer1[1] = 0xEF;
//		buffer1[2] = 0x00;    
//		SSD2828_W_Array(SigMode,channel,buffer1,0);   //???1′?è?Gamma  
//		delay_ms(5);
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		buffer[4] = Uart_Error_None;
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? D′Gamma×′ì?  ￡ook  
}

void RM6935X_auto_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[3];
	if(SigMode != CMD_Mode)
        SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);

		buffer1[0] = 0x02;
		buffer1[1] = 0xFE;
		buffer1[2] = 0x50;              // GAMMA1  page1
		SSD2828_W_Array(SigMode,channel,buffer1,0);	
		SSD2828_W_Reg(SigMode,channel,0xBC,0x02);
	
    for( i = 0;i<buffer[2];i++) 
    {	 
            SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|buffer[i+3]);  
            i++;
            delay_ms(2);
    }				
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}
void RM6935x_Write_51Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    SSD2828_W_Array(SigMode,channel,buffer,2);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}


void ProcessForIc3C( USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	//void *pdev;
	switch(buffer[1])
	{
		case 0x01:                                      //写寄存器
				RM6935x_Write_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				break;
		case 0x02:                                      //读寄存器
				RM6935x_Read_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				break;
		case 0x08:                                      //写Gamma
				RM6935x_Write_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				break;
		case 0x09:                                      //写Gamma
				RM6935x_Read_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				break;				
		case 0x0A:                                      //读Gamma
				RM6935x_Gamma_OTP_Start(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data); 
				break;
		case 0x0B: 
				RM6935x_Read_OTP_Time(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);	
				break;
		case 0x11: 
				RM6935x_Gamma_switch(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);	
				break;								
		case 0xDD:   									// auto Gamma数据
			    RM6935X_auto_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data); 
				break;		
		case 0x51:   									
			    RM6935x_Write_51Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data); 
				break;				
		default:break;
	}			
}